﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AutoMapper;
using EFEntity;
using ISerives;
using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore;
using Model;

namespace Serives
{
    public class salary_standard_Servies : IEsalary_standard_Servies
    {
        private readonly MyDbContext myDbContext;
        private readonly IMapper mapper;

        public salary_standard_Servies(MyDbContext myDbContext, IMapper mapper)
        {
            this.myDbContext = myDbContext;
            this.mapper = mapper;
        }
        public async Task<int> Add(Esalary_standard ess)
        {
            myDbContext.Eess.Add(ess);
            return await myDbContext.SaveChangesAsync();
        }

        public Task<int> Del(int id)
        {
            myDbContext.Remove(myDbContext.Eess.Where(e => e.ssd_id == id).FirstOrDefault());
            return myDbContext.SaveChangesAsync();
        }

        public async Task<List<Esalary_standard>> FenYe(FenYe fenye)
        {
            SqlParameter[] ps =
            {
                new SqlParameter(){ ParameterName="@pageSize",Value=fenye.PageSize},
                new SqlParameter(){ ParameterName="@keyName",Value="ssd_id"},
                new SqlParameter(){ ParameterName="@tableName",Value="salary_standard"},
                new SqlParameter(){ ParameterName="@where",Value=fenye.Where},
                new SqlParameter(){ ParameterName="@currentPage",Value=fenye.CurrentPage},
                new SqlParameter(){ ParameterName="@rows",Direction=ParameterDirection.Output,SqlDbType=SqlDbType.Int},
                new SqlParameter(){ ParameterName="@pages",Direction=ParameterDirection.Output,SqlDbType=SqlDbType.Int}
            };
            List<Esalary_standard> list = await myDbContext.Eess.FromSqlRaw("exec [dbo].[proc_FenYe] @pageSize, @keyName, @tableName, @where, @currentPage, @rows output, @pages output", ps).ToListAsync();
            fenye.Rows = (int)ps[5].Value;
            fenye.Pages = (int)ps[6].Value;
            //把EO-DTO
            List<Esalary_standard> list2 = new List<Esalary_standard>();
            foreach (Esalary_standard item in list)
            {
                Esalary_standard m = new Esalary_standard()
                {

                    ssd_id = item.ssd_id,
                    standard_id = item.standard_id,
                    standard_name = item.standard_name,
                    designer = item.designer,
                    register = item.register,
                    checker = item.checker,
                    changer = item.changer,
                    regist_time = item.regist_time,
                    check_time = item.check_time,
                    change_time = item.change_time,
                    salary_sum = item.salary_sum,
                    check_status = item.check_status,
                    change_status = item.change_status,
                    check_comment = item.check_comment,
                    remark = item.remark
                };
                list2.Add(m);
            }
            return list2;
        }

        public async Task<List<salary_standard>> Select()
        {
            List<salary_standard> list = new List<salary_standard>();
            await Task.Run(() =>
            {
                List<Esalary_standard> list2 = myDbContext.Eess.ToList();
                list = mapper.Map<List<salary_standard>>(list2);
            });
            return list;
        }

        public async Task<Esalary_standard> ShowId(int id)
        {
            Esalary_standard ess = new Esalary_standard();
          await  Task.Run(() =>
            {
                var m = myDbContext.Eess.Where(e => e.ssd_id == id).FirstOrDefault();
                ess.ssd_id = m.ssd_id;//: 主键，自动增长列
                ess.standard_id = m.standard_id;// : 薪酬标准单编号
                ess.standard_name = m.standard_name;// : 薪酬标准单名称
                ess.designer = m.designer;// : 制定者名字
                ess.register = m.register; //: 登记人
                ess.checker = m.checker; //: 复核人
                ess.changer = m.changer; //: 变更人
                ess.regist_time = m.regist_time; //: 登记时间
                ess.check_time = m.check_time; //: 复核时间
                ess.change_time = m.change_time;// : 变更时间
                ess.salary_sum = m.salary_sum;//: 薪酬总额
                ess.check_status = m.check_status;// : 是否经过复核
                ess.change_status = m.change_status; //: 更改状态
                ess.check_comment = m.check_comment;//: 复核意见
                ess.remark = m.remark;//: 备注
            });
            return ess;
        }
        /// <summary>
        /// 复核修改
        /// </summary>
        /// <param name="ess"></param>
        /// <returns></returns>
        public async Task<int> FH_Upd(Esalary_standard ess)
        {
            Esalary_standard m = myDbContext.Eess.Where(e => e.ssd_id == ess.ssd_id).FirstOrDefault();
            m.standard_id = ess.standard_id;// : 薪酬标准单编号
            m.standard_name = ess.standard_name;// : 薪酬标准单名称
            m.designer = ess.designer;// : 制定者名字
            m.checker = ess.checker; //: 复核人
            m.check_time = ess.check_time; //: 复核时间
            m.salary_sum = ess.salary_sum;//: 薪酬总额
            m.check_status = ess.check_status;// : 是否经过复核
            m.check_comment = ess.check_comment;//: 复核意见
            return await myDbContext.SaveChangesAsync();
        }

        public async Task<salary_standard> ShowId_sa(string id)
        {
            salary_standard ess = new salary_standard();
            await Task.Run(() =>
            {
                var m = myDbContext.Eess.Where(e => e.standard_id == id).FirstOrDefault();
                ess.ssd_id = m.ssd_id;
                ess.standard_id = m.standard_id;// : 薪酬标准单编号
                ess.standard_name = m.standard_name;// : 薪酬标准单名称
                ess.designer = m.designer;// : 制定者名字
                ess.register = m.register; //: 登记人
                ess.checker = m.checker; //: 复核人
                ess.changer = m.changer; //: 变更人
                ess.regist_time = m.regist_time; //: 登记时间
                ess.check_time = m.check_time; //: 复核时间
                ess.change_time = m.change_time;// : 变更时间
                ess.salary_sum = m.salary_sum;//: 薪酬总额
                ess.check_status = m.check_status;// : 是否经过复核
                ess.change_status = m.change_status; //: 更改状态
                ess.check_comment = m.check_comment;//: 复核意见
                ess.remark = m.remark;//: 备注
            });
            return ess;
        }

        public async Task<int> GB_Upd(Esalary_standard ess)
        {
            Esalary_standard m = myDbContext.Eess.Where(e => e.ssd_id == ess.ssd_id).FirstOrDefault();
            m.standard_id = ess.standard_id;// : 薪酬标准单编号
            m.standard_name = ess.standard_name;// : 薪酬标准单名称
            m.designer = ess.designer;// : 制定者名字
            m.changer = ess.changer; //: 变更人
            m.change_time = ess.change_time; //: 变更时间
            m.salary_sum = ess.salary_sum;//: 薪酬总额
            m.remark = ess.remark;//: 备注
            return await myDbContext.SaveChangesAsync();
        }

        public async Task<List<Esalary_standard>> ShowId_list(string id)
        {
            List<Esalary_standard> list = new List<Esalary_standard>();
            await Task.Run(() =>
            {
                List<Esalary_standard> list2 = myDbContext.Eess.Where(e=>e.standard_id==id).ToList();
                list = mapper.Map<List<Esalary_standard>>(list2);
            });
            return list;
        }
    }
}
